package com.bestcem.xm.member.dao.impl.mysql;

import cn.hutool.core.collection.CollUtil;
import com.bestcem.xm.member.dao.ExtendFieldDao;
import com.bestcem.xm.member.dao.mapper.ExtendFieldMapper;
import com.bestcem.xm.member.entity.mongo.ExtendField;
import com.bestcem.xm.member.entity.mysql.ExtendFieldEntity;
import com.bestcem.xm.member.entity.pojo.ExtendFieldDO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author dc.wang <dc.wang@idiaoyan.com>
 * @version V1.0
 * @description 扩展字段持久层实现类
 * @date 2021/7/20
 */
@Slf4j
@Repository
public class ExtendFieldDaoImpl implements ExtendFieldDao {

    @Autowired
    private ExtendFieldMapper extendFieldMapper;

    @Override
    public List<ExtendFieldDO> findByNames(String orgId, Integer memberType, Collection<String> names) {
        List<ExtendFieldEntity> extendFieldEntityList = extendFieldMapper.listByNames(orgId, memberType, names);
        return listToDO(extendFieldEntityList);
    }

    @Override
    public List<ExtendFieldDO> listMemberExtendField(String orgId, Integer memberType) {
        List<ExtendFieldEntity> extendFieldEntityList = extendFieldMapper.listByOrgIdAndMemberType(orgId, memberType);
        return listToDO(extendFieldEntityList);
    }

    @Override
    public Integer batchInsertField(@NotEmpty List<ExtendFieldDO> extendFieldDOList) {
        return extendFieldMapper.batchInsert(listToEntity(extendFieldDOList));
    }

    @Override
    public Integer batchDeleteFieldByIds(@NotNull String orgId, @NotEmpty Collection<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return 0;
        }

        return extendFieldMapper.batchDeleteByIds(orgId, ids);
    }

    @Override
    public void batchUpdateFields(@NotEmpty List<ExtendFieldDO> extendFieldDOList) {
        if (CollUtil.isNotEmpty(extendFieldDOList)) {
            for (ExtendFieldDO extendFieldDO : extendFieldDOList) {
                extendFieldMapper.updateExtendField(toEntity(extendFieldDO));
            }

        }
    }

    @Override
    public ExtendFieldDO findOneByNameAndOrgIdAndAttribute(String name, String orgId, Integer attribute) {
        ExtendFieldEntity extendFieldEntity = extendFieldMapper.getByNameAndOrgIdAndMemberType(name, orgId, attribute);
        return toDO(extendFieldEntity);
    }

    @Override
    public List<ExtendFieldDO> findByIds(Collection<String> ids) {
        List<ExtendFieldEntity> extendFieldEntityList = extendFieldMapper.listByIds(ids);
        return listToDO(extendFieldEntityList);
    }

    @Override
    public Integer countByOrgIdAndMemberTypeAndName(String orgId, Integer attribute, String name) {
        return extendFieldMapper.countByOrgIdAndMemberTypeAndName(orgId, attribute, name);
    }

    @Override
    public List<ExtendFieldDO> listByOrgIdAndMemberTypes(String orgSrcId, List<Integer> memberTypes) {
        List<ExtendFieldEntity> extendFieldEntityList = extendFieldMapper.listByOrgIdAndMemberTypes(orgSrcId, memberTypes);
        return listToDO(extendFieldEntityList);
    }

    /**
     * list : entity -> DO
     *
     * @param extendFieldEntityList entityList
     * @return DOList
     */
    private List<ExtendFieldDO> listToDO(List<ExtendFieldEntity> extendFieldEntityList) {
        return extendFieldEntityList.stream().map(this::toDO).collect(Collectors.toList());
    }

    /**
     * list : DO -> entity
     *
     * @param extendFieldDOList DOList
     * @return entityList
     */
    private List<ExtendFieldEntity> listToEntity(List<ExtendFieldDO> extendFieldDOList) {
        return extendFieldDOList.stream().map(this::toEntity).collect(Collectors.toList());
    }

    /**
     * entity -> DO
     *
     * @param extendFieldEntity entity
     * @return DO
     */
    private ExtendFieldDO toDO(ExtendFieldEntity extendFieldEntity) {
        ExtendFieldDO extendFieldDO = new ExtendFieldDO();
        BeanUtils.copyProperties(extendFieldEntity, extendFieldDO);
        return extendFieldDO;
    }

    /**
     * DO -> entity
     *
     * @param extendFieldDO DO
     * @return entity
     */
    private ExtendFieldEntity toEntity(ExtendFieldDO extendFieldDO) {
        ExtendFieldEntity extendFieldEntity = new ExtendFieldEntity();
        BeanUtils.copyProperties(extendFieldDO, extendFieldEntity);
        return extendFieldEntity;
    }
        private ExtendFieldEntity convert(ExtendField extendField){
            ExtendFieldEntity extendFieldEntity = new ExtendFieldEntity();
            extendFieldEntity.setFieldId(extendField.getId());
            extendFieldEntity.setOrgId(extendField.getOrgId().toString());
            extendFieldEntity.setMemberType(extendField.getMemberType());
            extendFieldEntity.setName(extendField.getName());
            extendFieldEntity.setType(extendField.getType());
            if(extendField.getCreateTime() != null){
                extendFieldEntity.setCreateTime(extendField.getCreateTime());
            }
            if(extendField.getUpdateTime() != null){
                extendFieldEntity.setUpdateTime(extendField.getUpdateTime());
            }
            return extendFieldEntity;
        }
}
